package com.voice.core.api.model;

import com.voice.core.api.enums.HttpMethod;
import com.voice.core.api.enums.Scheme;
import lombok.Data;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Data
public final class ApiRequest extends ApiHttpMessage {
    private Scheme scheme;
    private HttpMethod method;
    private String host;
    private String path;
    private String url;

    private String signatureMethod = "HmacSHA256";
    private Map<String, String> pathParams;
    private Map<String, String> querys;
    private Map<String, String> formParams;
    private Map<String, File> files;

    public ApiRequest(HttpMethod method, String path) {
        this.pathParams = new HashMap();
        this.querys = new HashMap();
        this.formParams = new HashMap();
        this.files = new HashMap();
        this.method = method;
        this.path = path;
    }

    public ApiRequest(HttpMethod method, String path, byte[] body) {
        this(method,path);
        this.body = body;
    }

    public void addPathParams(String name, String value){
        this.pathParams.put(name,value);
    }

    public void addPathParams(Map<String, String> pathParams){
        this.pathParams.putAll(pathParams);
    }

    public void addQuerys(String name, String value){
        this.querys.put(name,value);
    }

    public void addQuerys(Map<String, String> querys){
        if(querys!=null) this.querys.putAll(querys);
    }

    public void addFormParams(String name, String value){
        this.formParams.put(name,value);
    }

    public void addFormParams(Map<String, String> formParams){
        if(formParams!=null) this.formParams.putAll(formParams);
    }

    public void addFiles(String name, File file){
        this.files.put(name,file);
    }

    public void addFiles(Map<String, File> files){
        this.files.putAll(files);
    }


    public ApiRequest duplicate() {
        ApiRequest apiRequest = new ApiRequest(this.method, this.path, this.body);
        if (null != this.host) {
            apiRequest.host = this.host;
        }
        if (null != this.url) {
            apiRequest.url = this.url;
        }
        apiRequest.pathParams = new HashMap();
        apiRequest.pathParams.putAll(this.pathParams);
        apiRequest.headers = new HashMap();
        apiRequest.headers.putAll(this.headers);
        apiRequest.querys = new HashMap();
        apiRequest.querys.putAll(this.querys);
        apiRequest.formParams = new HashMap();
        apiRequest.formParams.putAll(this.formParams);
        if (null != this.signatureMethod) {
            apiRequest.signatureMethod = this.signatureMethod;
        }
        return apiRequest;
    }
}
